﻿using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.Utils;

namespace WCS.Entity
{
    [SugarTable("WCS_Location_Info")]
    public class LocationInfo
    {
        /// <summary>
        /// 站台状态
        /// </summary>
        public int state { get; set; }

        /// <summary>
        /// 当前位置
        /// </summary>
        public string StationLocation { get; set; }

        /// <summary>
        /// 物料托盘
        /// </summary>
        public string MaterialInfo { get; set; }

        /// <summary>
        /// 托盘1
        /// </summary>
        public string BarCode1 { get; set; }

        /// <summary>
        /// 托盘2
        /// </summary>
        public string BarCode2 { get; set; }

        /// <summary>
        /// 托盘1的数据内容
        /// </summary>
        public string BarCode1Data { get; set; }

        /// <summary>
        /// 托盘2的数据内容
        /// </summary>
        public string BarCode2Data { get; set; }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper _SqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// 删除站台库存信息
        /// </summary>
        /// <param name="stationLocation"></param>
        public static void DeleteLocationInfo(string stationLocation)
        {
            //更新库存信息 在库
            _SqlSugarDapper.DB.Deleteable<LocationInfo>()
                .Where(w => w.StationLocation == stationLocation && w.state == (int)StationState.出库占用).ExecuteCommand();
        }

        /// <summary>
        /// 添加站台库存信息
        /// </summary>
        /// <param name="stationLocation"></param>
        /// <param name="materialInfo"></param>
        public static void AddStatioInfo(LocationInfo locationInfo)
        {
            //添加库位信息 入库占用
            _SqlSugarDapper.DB.Insertable(locationInfo).ExecuteCommand();
        }

        /// <summary>
        /// 修改站台库存信息
        /// </summary>
        /// <param name="stationLocation"></param>
        public static void ChangeStatioState(string stationLocation, StationState stationState, string MaterialInfo = null)
        {
            //更新库存信息 出库占用
            _SqlSugarDapper.DB.Updateable<LocationInfo>()
                .SetColumns(w => w.state == (int)stationState)
                .SetColumnsIF(!MaterialInfo.IsNullOrEmpty(), w => w.MaterialInfo == MaterialInfo)
                .Where(w => w.StationLocation == stationLocation).ExecuteCommand();
        }

        /// <summary>
        /// 修改站台库存信息
        /// </summary>
        /// <param name="stationLocation"></param>
        public static void ChangeStatioState(string stationLocation, string taskCode)
        {
            //更新库存信息 出库占用
            _SqlSugarDapper.DB.Updateable<LocationInfo>()
                .SetColumns(w => w.state == Convert.ToInt32(taskCode))
                .Where(w => w.StationLocation == stationLocation).ExecuteCommand();
        }

        /// <summary>
        /// 根据需要物料获取可用站台位
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static string GetAvailableStation(string material, int binCode)
        {
            return _SqlSugarDapper.DB.Queryable<LocationInfo>()
                .Where(w => SqlFunc.Subqueryable<AreaLocation>().Where(s => s.StationNo == w.StationLocation && s.AreaCode == "4000" && s.BinCode == binCode).Any() &&
                w.MaterialInfo == material && w.state == (int)StationState.在库).First()?.StationLocation;
        }

        public static string GetInStation(string material, int binCode)
        {
            return _SqlSugarDapper.DB.Queryable<LocationInfo>()
                .Where(w => SqlFunc.Subqueryable<AreaLocation>().Where(s => s.StationNo == w.StationLocation && s.AreaCode == "4000" && s.BinCode == binCode).Any() &&
                w.MaterialInfo == material && w.state == (int)StationState.在库).First()?.StationLocation;
        }

        /// <summary>
        /// 获取一个可用站台然后入库
        /// </summary>
        /// <returns></returns>
        public static LocationInfo GetAvailableStation()
        {
            return _SqlSugarDapper.DB.Queryable<LocationInfo>()
                .Where(w => SqlFunc.Subqueryable<AreaLocation>().Where(s => s.StationNo == w.StationLocation && s.AreaCode == "4000").Any() &&
                w.state == (int)StationState.在库).First();
        }

        /// <summary>
        /// 获取地堆库存信息
        /// </summary>
        /// <returns></returns>
        public static LocationInfo GetStationInfo(string station)
        {
            return _SqlSugarDapper.DB.Queryable<LocationInfo>().Where(w => w.StationLocation == station).First();
        }

        /// <summary>
        /// 查找空闲缓存库位
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static string GetCacheBit(string areaCode, int binCode, string material)
        {
            //查找可用库位
            return _SqlSugarDapper.DB.Queryable<AreaLocation>()
                .Where(w => w.AreaCode == areaCode && w.BinCode == binCode && w.Enable == "1" && w.MaterialType == material && SqlFunc.Subqueryable<LocationInfo>().Where(f => w.StationNo == f.StationLocation).NotAny()
                ).First()?.StationNo;
        }
    }
}
